//
//  RecentHistoryVC.m
//  timelyRain
//
//  Created by Jiafei Wei on 16/7/23.
//  Copyright © 2016年 Jiafei Wei. All rights reserved.
//

#import "RecentHistoryVC.h"
#import "TCallKeyborad.h"
#import "MBTPre.h"
#import "HttpTool.h"
#import "TimeReuest.h"
#import "MJExtension.h"
#import "RunSucessInfo.h"
#import "TimeChartRecord.h"
#import "ChartResut.h"
#import "MessageShow.h"
#import "TMessageCell.h"
#import "DataTool.h"
#import "BindTool.h"
@interface RecentHistoryVC ()<UITableViewDelegate,UITableViewDataSource,TCallKeyboradDelegate,EMChatManagerDelegate>
{
    CGRect rect;
    NSMutableArray *dataArr;
}
@property (weak, nonatomic) IBOutlet UIView *keyB;

@property (weak, nonatomic) IBOutlet UIButton *openKey;
- (IBAction)open:(id)sender;
@property (weak, nonatomic) IBOutlet UITableView *table;

@end

@implementation RecentHistoryVC

- (void)viewDidLoad {
    [super viewDidLoad];
    _table.delegate=self;
    _table.dataSource=self;
    TCallKeyborad *keyboard =[TCallKeyborad headerView];
    keyboard.delegate =self;
    //_keyB.frame =CGRectMake(0, SCREEN_H, _keyB.frame.size.width,_keyB.frame.size.height);
    [_keyB addSubview:keyboard];
    MessageShow *showInfo = [[MessageShow alloc]init];
    showInfo.name =@"黄经理";
    showInfo.phoneNumber=@"18927482671";
    showInfo.callTime=@"上午 12:20";
    showInfo.missSMSNumber=@"2";
    showInfo.missCallNumber=@"2";
    dataArr =[NSMutableArray array];
    [dataArr addObject:showInfo];
    [_table reloadData];
    //[self registerNotifications];
}
-(void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    rect =_keyB.frame;
}
-(void)contactkeyBoard:(UIButton*)b{//键盘的操作
        if (b.tag==10) {
    
        }else if(b.tag==11){
    
        }else if(b.tag==12){//收缩
            if (!_keyB.hidden) {
                [UIView animateWithDuration:1.0 // 动画时长
                                      delay:0.0 // 动画延迟
                     usingSpringWithDamping:0.5// 类似弹簧振动效果 0~1
                      initialSpringVelocity:3.0 // 初始速度
                                    options:UIViewAnimationOptionCurveEaseInOut // 动画过渡效果
                                 animations:^{
                                     _keyB.frame =CGRectMake(0, SCREEN_H, _keyB.frame.size.width,_keyB.frame.size.height);
                                 } completion:^(BOOL finished) {
                                     _keyB.hidden=YES;
                                     _openKey.hidden =NO;
                                 }];
            }
        }else if(b.tag==13){//打电话
    
        }else if(b.tag==14){//发短信
    
        }else if(b.tag==15){
            //删除
        }else {
          //返回数字
        }
}
- (IBAction)open:(id)sender {
    if (_keyB.hidden) {
        _keyB.hidden=NO;
        [UIView animateWithDuration:1.0 // 动画时长
                              delay:0.0 // 动画延迟
             usingSpringWithDamping:0.5// 类似弹簧振动效果 0~1
              initialSpringVelocity:3.0 // 初始速度
                            options:UIViewAnimationOptionCurveEaseInOut // 动画过渡效果
                         animations:^{
                             _keyB.frame =rect;
                         } completion:^(BOOL finished) {
                             _openKey.hidden =YES;
                         }];
    }
}
//开个线程去才行
#define SMS @"sms"
#define CALL @"call_record"
#define TXT @"txt"
#define AUDIO @"audio"
#define PIC @"picture"
#define bindWathchPhone @""
-(void)getChartRecodeFromFB{
    //重数据库中获取绘画列表
}
//开始画界面
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TMessageCell *messageCell = [TMessageCell setTMessageCellWithTableView:tableView];
    messageCell.showData=[dataArr objectAtIndex:indexPath.row];
    return messageCell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

}
- (void)didReceiveCmdMessages:(NSArray *)aCmdMessages//收到了
{
    NSLog(@"我是历史聊天记录啊,什么时候啊");
    for (EMMessage *message in aCmdMessages) {
        EMCmdMessageBody *cmdBody =(EMCmdMessageBody *)message.body;
        NSLog(@"cmdBody.action ====%@",cmdBody.action);
        NSString *action =cmdBody.action;
        if ([action isEqualToString:@"update_char_record"]) {
            
        }else if([action isEqualToString:@"recevie_txt"]){
            //获取文本消息
            NSDictionary *dataAll=message.ext;
            NSDictionary *data =[dataAll objectForKey:@"data"];
            NSString *from =[data objectForKey:@"from"];
            NSString *to =[data objectForKey:@"to"];
            NSString *content =[data objectForKey:@"content"];
            NSNumber *time =[data objectForKey:@"date"];
            NSString *msgid =[data objectForKey:@"msgid"];
            NSLog(@"from===%@,to===%@,content==%@",from,to,content);
            //插入数据库中去
            EMConversation *conversation =[[EMClient sharedClient].chatManager getConversation:from type:EMConversationTypeChat createIfNotExist:YES];
                        EMMessage *message =[conversation loadMessageWithId:msgid error:nil];
                        if (message==nil) {
                             EMMessage *insertMessage = [EaseSDKHelper sendTextMessage:content to:from messageType:EMChatTypeChat messageExt:nil];
                            [insertMessage setMessageId:msgid];
                            [[EMClient sharedClient].chatManager importMessages:@[insertMessage] completion:nil];
                            insertMessage.direction=EMMessageDirectionReceive;
                            insertMessage.status=EMMessageStatusSuccessed;
                            [conversation insertMessage:insertMessage error:nil];
                        }else{
                            NSLog(@"已经蠢仔了");
                        }
        }else if([action isEqualToString:@"action_setting"]){
            
        }else if([action isEqualToString:@"action_notify"]){
            
        }else if([action isEqualToString:@"action_intercept_list"]){//拦截
            
        }else if([action isEqualToString:@"call_phone"]){
            
        }else if([action isEqualToString:@"action_call_offhook"]){
            
        }
    }
}
-(void)registerNotifications{
    [self unregisterNotifications];
    [[EMClient sharedClient].chatManager addDelegate:self delegateQueue:nil];
}

-(void)unregisterNotifications{
    [[EMClient sharedClient].chatManager removeDelegate:self];
}
-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:YES];
    //[self unregisterNotifications];
}
@end
